package 常用api;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Set;

public class java8新增时间 {
    public static void main(String[] args) {
        LocalDate date = LocalDate.now();
//        查看相关信息
        System.out.println(date);
        System.out.println(date.getYear());
        System.out.println(date.getMonth());
        System.out.println(date.getMonthValue());
//        还有类似的就不写了
        System.out.println(date.getDayOfWeek().getValue());
//        修改相关信息(不会修改原来的日期,会返回一个新日期,就像string一样)
        System.out.println(date.withYear(10));
        System.out.println(date.plusYears(10));
        System.out.println(date.minusYears(10));
//获取指定的localdate对象
        System.out.println(LocalDate.of(2019, 10, 25));
//        判断两个日期的关系
        System.out.println(LocalDate.of(2019, 10, 25).isBefore(date));
//        localdatetime和localdate差不多的用法
        ZoneId zoneId = ZoneId.systemDefault();
        System.out.println(zoneId.getId());
        Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
//        for (String availableZoneId : availableZoneIds) {
//            System.out.println(availableZoneId);
//        }
        ZoneId zonedDateTime = ZoneId.of("America/Los_Angeles");
        System.out.println(ZonedDateTime.now(zonedDateTime));
        System.out.println(ZonedDateTime.now(Clock.systemUTC()));
        Instant now = Instant.now();//这个对象也不可变
//        获取总秒数
        Long seconds = now.getEpochSecond();
//        获取不够一秒的纳秒数
        Integer nano = now.getNano();
        System.out.println(seconds);
        System.out.println(nano);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy:MM:dd");
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);
        System.out.println(localDateTime.format(dateTimeFormatter));
        String str1 = localDateTime.format(dateTimeFormatter);
        System.out.println(str1);
        String str2 = dateTimeFormatter.format(localDateTime);
        System.out.println(str2);
        String time = "2019:10:25";
//        下面如果要用localdatetime的话格式器必须有时分秒的格式
        LocalDate parse = LocalDate.parse(time, dateTimeFormatter);
        System.out.println(parse);
        LocalDate localDate1 = LocalDate.of(2019, 2, 25);
        LocalDate localDate2 = LocalDate.of(2020, 2, 29);
//        这里是用后面那个日期减去前面那个日期
        Period period = Period.between(localDate1, localDate2);
//        另外这里获取两个日期间隔多少天和多少月都是不考虑年的
        System.out.println(period.getDays());
        System.out.println(period.getMonths());
        Duration duration = Duration.between(localDate1.atStartOfDay(), localDate2.atStartOfDay());
        System.out.println(duration.getSeconds());
        System.out.println(duration.toDays());
        System.out.println(duration.getNano());

    }
}
